﻿namespace AnyOffice.WebControls
{
    using System;
    using System.Collections.Specialized;
    using System.ComponentModel;
    using System.Runtime.CompilerServices;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    [ToolboxData("<{0}:HFCalendar runat=server></{0}:HFCalendar>"), DefaultProperty("Text")]
    public class HFCalendar : WebControl, IPostBackDataHandler
    {
        private NotNUllType _NotNUllType = NotNUllType.False;
        private string size;
        private string strChineseName = "";
        private string strFieldLength = "";
        private string strPropertyName = "";
        private string strPropertyValue = "";

        public event EventHandler TextChanged;

        public virtual bool LoadPostData(string postDataKey, NameValueCollection postCollection)
        {
            string text = this.Text;
            string str2 = postCollection[postDataKey];
            if (!((text != null) && text.Equals(str2)))
            {
                this.Text = str2;
                return true;
            }
            return false;
        }

        protected virtual void OnTextChanged(EventArgs e)
        {
            if (this.TextChanged != null)
            {
                this.TextChanged(this, e);
            }
        }

        public virtual void RaisePostDataChangedEvent()
        {
            this.OnTextChanged(EventArgs.Empty);
        }

        protected override void Render(HtmlTextWriter output)
        {
            output.AddAttribute(HtmlTextWriterAttribute.Id, this.UniqueID);
            output.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID);
            output.AddAttribute(HtmlTextWriterAttribute.Class, this.CssClass);
            output.AddAttribute(HtmlTextWriterAttribute.Value, this.Text);
            output.AddAttribute(HtmlTextWriterAttribute.Size, this.Size);
            output.AddAttribute(HtmlTextWriterAttribute.Style, "cursor:hand");
            output.AddAttribute(HtmlTextWriterAttribute.Type, "text");
            output.AddAttribute(HtmlTextWriterAttribute.ReadOnly, "true");
            output.AddAttribute(HtmlTextWriterAttribute.Onclick, "javascript:this.focus()");
            output.AddAttribute("onFocus", "fPopCalendar(this,this,PopCal); return false;");
            output.RenderBeginTag(HtmlTextWriterTag.Input);
            output.RenderEndTag();
            if (!this.Page.IsClientScriptBlockRegistered("clientScript"))
            {
                output.WriteLine("<script language=\"JavaScript\">");
                output.WriteLine("var gdCtrl = new Object();");
                output.WriteLine("var gcGray = \"#808080\";");
                output.WriteLine("var gcToggle = \"highlight\";");
                output.WriteLine("var gcBG = \"threedface\";");
                output.WriteLine("var gMonths = new Array(\"一月\",\"二月\",\"三月\",\"四月\",\"五月\",\"六月\",\"七月\",\"八月\",\"九月\",\"十月\",\"十一月\",\"十二月\");");
                output.WriteLine("var gdCurDate = new Date();");
                output.WriteLine("var giYear = gdCurDate.getFullYear();");
                output.WriteLine("var giMonth = gdCurDate.getMonth()+1;");
                output.WriteLine("var giDay = gdCurDate.getDate();");
                output.WriteLine("var sxYear = giYear;");
                output.WriteLine("var sxMonth = giMonth;");
                output.WriteLine("var sxDay = giDay;");
                output.WriteLine("var sxDatestr = gdCtrl.value;");
                output.WriteLine("if (sxDatestr != \"\"){");
                output.WriteLine("var sxDate = new Date(sxDatestr);");
                output.WriteLine("sxYear = sxDate.getFullYear();");
                output.WriteLine("}");
                output.WriteLine("var VicPopCal = new Object();");
                output.WriteLine("function mouseover(obj){");
                output.WriteLine("obj.style.borderTop = 'buttonshadow 1px solid';");
                output.WriteLine("obj.style.borderLeft = 'buttonshadow 1px solid';");
                output.WriteLine("obj.style.borderRight = 'buttonhighlight 1px solid';");
                output.WriteLine("obj.style.borderBottom = 'buttonhighlight 1px solid';");
                output.WriteLine("}");
                output.WriteLine("function mouseout(obj){");
                output.WriteLine("obj.style.borderTop = 'buttonhighlight 1px solid';");
                output.WriteLine("obj.style.borderLeft = 'buttonhighlight 1px solid';");
                output.WriteLine("obj.style.borderRight = 'buttonshadow 1px solid';");
                output.WriteLine("obj.style.borderBottom = 'buttonshadow 1px solid';");
                output.WriteLine("}");
                output.WriteLine("function mousedown(obj){");
                output.WriteLine("obj.style.borderTop = 'buttonshadow 1px solid';");
                output.WriteLine("obj.style.borderLeft = 'buttonshadow 1px solid';");
                output.WriteLine("obj.style.borderRight = 'buttonhighlight 1px solid';");
                output.WriteLine("obj.style.borderBottom = 'buttonhighlight 1px solid';");
                output.WriteLine("}");
                output.WriteLine("function mouseup(obj){");
                output.WriteLine("obj.style.borderTop = 'buttonhighlight 1px solid';");
                output.WriteLine("obj.style.borderLeft = 'buttonhighlight 1px solid';");
                output.WriteLine("obj.style.borderRight = 'buttonshadow 1px solid';");
                output.WriteLine("obj.style.borderBottom = 'buttonshadow 1px solid';");
                output.WriteLine("}");
                output.WriteLine("function fPopCalendar(popCtrl, dateCtrl, popCal){");
                output.WriteLine("VicPopCal = popCal;");
                output.WriteLine("gdCtrl = dateCtrl;");
                output.WriteLine("fSetYearMon(giYear, giMonth);");
                output.WriteLine("var point = fGetXY(popCtrl);");
                output.WriteLine("with (VicPopCal.style) {left = point.x;top  = point.y+popCtrl.offsetHeight+1;visibility = 'visible';}");
                output.WriteLine("VicPopCal.focus();");
                output.WriteLine("}");
                output.WriteLine("function fSetDate(iYear, iMonth, iDay){");
                output.WriteLine("if ((iYear == 0) && (iMonth == 0) && (iDay == 0)){");
                output.WriteLine("gdCtrl.value = \"\";");
                output.WriteLine("}");
                output.WriteLine("else{");
                output.WriteLine("if (iMonth < 10){iMonth = \"0\"+iMonth;}");
                output.WriteLine("if (iDay < 10){iDay = \"0\"+iDay;}");
                output.WriteLine("gdCtrl.value = iYear+\"-\"+iMonth+\"-\"+iDay;");
                output.WriteLine("}");
                output.WriteLine("VicPopCal.style.visibility = \"hidden\";");
                output.WriteLine("}");
                output.WriteLine("function fSetSelected(aCell){");
                output.WriteLine("var iOffset = 0;");
                output.WriteLine("var iYear = parseInt(document.all.tbSelYear.value);");
                output.WriteLine("var iMonth = parseInt(document.all.tbSelMonth.value);");
                output.WriteLine("aCell.bgColor = gcBG;");
                output.WriteLine("with (aCell.children[\"cellText\"]){");
                output.WriteLine("var iDay = parseInt(innerText);");
                output.WriteLine("if (color==gcGray){iOffset = (Victor<10)?-1:1;}");
                output.WriteLine("iMonth += iOffset;");
                output.WriteLine("if (iMonth<1) {\tiYear--; iMonth = 12;}else{if (iMonth>12){iYear++;iMonth = 1;}}");
                output.WriteLine("}");
                output.WriteLine("fSetDate(iYear, iMonth, iDay);");
                output.WriteLine("}");
                output.WriteLine("function Point(iX, iY){this.x = iX;this.y = iY;}");
                output.WriteLine("function fBuildCal(iYear, iMonth){");
                output.WriteLine("var aMonth=new Array();");
                output.WriteLine("for(i=1;i<7;i++){aMonth[i]=new Array(i);}");
                output.WriteLine("var dCalDate=new Date(iYear, iMonth-1, 1);");
                output.WriteLine("var iDayOfFirst=dCalDate.getDay();");
                output.WriteLine("var iDaysInMonth=new Date(iYear, iMonth, 0).getDate();");
                output.WriteLine("var iOffsetLast=new Date(iYear, iMonth-1, 0).getDate()-iDayOfFirst+1;");
                output.WriteLine("var iDate = 1;");
                output.WriteLine("var iNext = 1;");
                output.WriteLine("for (d = 0; d < 7; d++){aMonth[1][d] = (d<iDayOfFirst)?-(iOffsetLast+d):iDate++;}");
                output.WriteLine("for (w = 2; w < 7; w++){for (d = 0; d < 7; d++){aMonth[w][d] = (iDate<=iDaysInMonth)?iDate++:-(iNext++);}}");
                output.WriteLine("return aMonth;");
                output.WriteLine("}");
                output.WriteLine("function fDrawCal(iYear, iMonth, iDay, iCellWidth, iDateTextSize) {");
                output.WriteLine("var WeekDay = new Array(\"日\",\"一\",\"二\",\"三\",\"四\",\"五\",\"六\");");
                output.WriteLine("var styleTD = \" bgcolor='\"+gcBG+\"' width='\"+iCellWidth+\"' bordercolor='\"+gcBG+\"' valign='middle' align='center' style='font-size: 12px;background: buttonface;border-top: buttonhighlight 1px solid;border-left: buttonhighlight 1px solid;border-right: buttonshadow 1px solid;\tborder-bottom: buttonshadow 1px solid;\";");
                output.WriteLine("with (document) {");
                output.WriteLine("write(\"<tr align='center'>\");");
                output.WriteLine("for(i=0; i<7; i++){write(\"<td height='20' \"+styleTD+\"color:#990099' >\" + WeekDay[i] + \"</td>\");}");
                output.WriteLine("write(\"</tr>\");");
                output.WriteLine("for (w = 1; w < 7; w++) {");
                output.WriteLine("write(\"<tr align='center'>\");");
                output.WriteLine("for (d = 0; d < 7; d++) {");
                output.WriteLine("write(\"<td width='10%' height='15' id=calCell \"+styleTD+\"cursor:hand;' onmouseover='mouseover(this)' onmouseout='mouseout(this)' onmousedown='mousedown(this)' onmouseup='mouseup(this)' onclick='fSetSelected(this)'>\");");
                output.WriteLine("write(\"<font style='font-size: 13px;' id=cellText Victor='Liming Weng'> </font>\");");
                output.WriteLine("write(\"</td>\");");
                output.WriteLine("}");
                output.WriteLine("write(\"</tr>\");");
                output.WriteLine("}");
                output.WriteLine("}");
                output.WriteLine("}");
                output.WriteLine("function fUpdateCal(iYear, iMonth) {");
                output.WriteLine("sxYear = iYear;");
                output.WriteLine("sxMonth = iMonth;");
                output.WriteLine("yeartd1.innerText = sxYear + \"年\";");
                output.WriteLine("monthtd1.innerText = gMonths[sxMonth-1];");
                output.WriteLine("myMonth = fBuildCal(iYear, iMonth);");
                output.WriteLine("var i = 0;");
                output.WriteLine("for (w = 0; w < 6; w++){");
                output.WriteLine("for (d = 0; d < 7; d++){");
                output.WriteLine("with (cellText[(7*w)+d]) {");
                output.WriteLine("Victor = i++;");
                output.WriteLine("if (myMonth[w+1][d]<0) {");
                output.WriteLine("color = gcGray;");
                output.WriteLine("innerText = -myMonth[w+1][d];");
                output.WriteLine("}else{");
                output.WriteLine("color = ((d==0)||(d==6))?\"red\":\"black\";");
                output.WriteLine("innerText = myMonth[w+1][d];");
                output.WriteLine("}");
                output.WriteLine("}");
                output.WriteLine("}");
                output.WriteLine("}");
                output.WriteLine("}");
                output.WriteLine("function fSetYearMon(iYear, iMon){");
                output.WriteLine("sxYear = iYear;");
                output.WriteLine("sxMonth = iMon;");
                output.WriteLine("yeartd1.innerText = sxYear + \"年\";");
                output.WriteLine("monthtd1.innerText = gMonths[sxMonth-1];");
                output.WriteLine("document.all.tbSelMonth.options[iMon-1].selected = true;");
                output.WriteLine("for (i = 0; i < document.all.tbSelYear.length; i++){");
                output.WriteLine("if (document.all.tbSelYear.options[i].value == iYear){");
                output.WriteLine("document.all.tbSelYear.options[i].selected = true;");
                output.WriteLine("}");
                output.WriteLine("}");
                output.WriteLine("fUpdateCal(iYear, iMon);");
                output.WriteLine("}");
                output.WriteLine("function fPrevMonth(){");
                output.WriteLine("var iMon = document.all.tbSelMonth.value;");
                output.WriteLine("var iYear = document.all.tbSelYear.value;");
                output.WriteLine("if (--iMon<1) {");
                output.WriteLine("iMon = 12;");
                output.WriteLine("iYear--;");
                output.WriteLine("}");
                output.WriteLine("fSetYearMon(iYear, iMon);");
                output.WriteLine("}");
                output.WriteLine("function fNextMonth(){");
                output.WriteLine("var iMon = document.all.tbSelMonth.value;");
                output.WriteLine("var iYear = document.all.tbSelYear.value;");
                output.WriteLine("if (++iMon>12) {");
                output.WriteLine("iMon = 1;");
                output.WriteLine("iYear++;");
                output.WriteLine("}");
                output.WriteLine("fSetYearMon(iYear, iMon);");
                output.WriteLine("}");
                output.WriteLine("function fGetXY(aTag){");
                output.WriteLine("var oTmp = aTag;");
                output.WriteLine("var pt = new Point(0,0);");
                output.WriteLine("do {");
                output.WriteLine("pt.x += oTmp.offsetLeft;");
                output.WriteLine("pt.y += oTmp.offsetTop;");
                output.WriteLine("oTmp = oTmp.offsetParent;");
                output.WriteLine("} while(oTmp.tagName!=\"BODY\");");
                output.WriteLine("return pt;");
                output.WriteLine("}");
                output.WriteLine("with (document){");
                output.WriteLine("write(\"<Div id='PopCal' onclick='event.cancelBubble=true' style='POSITION:absolute; VISIBILITY: hidden; bordercolor:#000000;border:2px ridge;width:10;z-index:100;'>\");");
                output.WriteLine("write(\"<table id='popTable' border='1' bgcolor='#eeede8' cellpadding='0' cellspacing='0' style='font-size:12px'>\");");
                output.WriteLine("write(\"<TR>\");");
                output.WriteLine("write(\"<td valign='middle' align='center' style='cursor:default'>\");");
                output.WriteLine("write(\"<table width='176' border='0' cellpadding='0' cellspacing='0'>\");");
                output.WriteLine("write(\"<tr align='center'>\");");
                output.WriteLine("write(\"<td height='22' width='20' name='PrevMonth' style='font-family:\\\"webdings\\\";font-size:15px' onClick='fPrevMonth()' onmouseover='this.style.color=\\\"#ff9900\\\"' onmouseout='this.style.color=\\\"\\\"'>3</td>\");");
                output.WriteLine("write(\"<td width='64' id='yeartd1' style='font-size:12px' onmouseover='yeartd1.style.display=\\\"none\\\";yeartd2.style.display=\\\"\\\";' onmouseout='this.style.background=\\\"\\\"'>\");");
                output.WriteLine("write(sxYear + \"年\");");
                output.WriteLine("write(\"</td>\");");
                output.WriteLine("write(\"<td width='64' id='yeartd2' style='display:none' onmouseout='yeartd2.style.display=\\\"none\\\";yeartd1.style.display=\\\"\\\";'>\");");
                output.WriteLine("write(\"<SELECT style='width:64px;font-size: 12px;font-family: 宋体;' id='tbSelYear' onChange='fUpdateCal(document.all.tbSelYear.value, document.all.tbSelMonth.value);yeartd2.style.display=\\\"none\\\";yeartd1.style.display=\\\"\\\";' Victor='Won'>\");");
                output.WriteLine("for(i=1930;i<2015;i++){");
                output.WriteLine("write(\"<OPTION value='\"+i+\"'>\"+i+\"年</OPTION>\");");
                output.WriteLine("}");
                output.WriteLine("write(\"</SELECT>\");");
                output.WriteLine("write(\"</td>\");");
                output.WriteLine("write(\"<td width='72' id='monthtd1' style='font-size:12px' onmouseover='monthtd1.style.display=\\\"none\\\";monthtd2.style.display=\\\"\\\";' onmouseout='this.style.background=\\\"\\\"'>\");");
                output.WriteLine("write(gMonths[sxMonth-1]);");
                output.WriteLine("write(\"</td>\");");
                output.WriteLine("write(\"<td width='72' id='monthtd2' style='display:none' onmouseout='monthtd2.style.display=\\\"none\\\";monthtd1.style.display=\\\"\\\";'>\");");
                output.WriteLine("write(\"<select style='width:72px;font-size: 12px;font-family: 宋体;' id='tbSelMonth' onChange='fUpdateCal(document.all.tbSelYear.value, document.all.tbSelMonth.value);monthtd2.style.display=\\\"none\\\";monthtd1.style.display=\\\"\\\";' Victor='Won'>\");");
                output.WriteLine("for (i=0; i<12; i++){");
                output.WriteLine("write(\"<option value='\"+(i+1)+\"'>\"+gMonths[i]+\"</option>\");");
                output.WriteLine("}");
                output.WriteLine("write(\"</SELECT>\");");
                output.WriteLine("write(\"</td>\");");
                output.WriteLine("write(\"<td width='20' name='PrevMonth' style='font-family:\\\"webdings\\\";font-size:15px' onclick='fNextMonth()' onmouseover='this.style.color=\\\"#ff9900\\\"' onmouseout='this.style.color=\\\"\\\"'>4</td>\");");
                output.WriteLine("write(\"</tr>\");");
                output.WriteLine("write(\"</table>\");");
                output.WriteLine("write(\"</td></TR><TR><td align='center'>\");");
                output.WriteLine("write(\"<DIV style='background-color:teal;'><table width='100%' border='0' bgcolor='threedface' cellpadding='0' cellspacing='0'>\");");
                output.WriteLine("fDrawCal(giYear, giMonth, giDay, 19, 14);");
                output.WriteLine("write(\"</table></DIV>\");");
                output.WriteLine("write(\"</td></TR><TR><TD height='20' align='center' valign='bottom'>\");");
                output.WriteLine("write(\"<font style='cursor:hand;font-size:12px' onclick='fSetDate(0,0,0)' onMouseOver='this.style.color=\\\"#0033FF\\\"' onMouseOut='this.style.color=0'>清空</font>\");");
                output.WriteLine("write(\"&nbsp;&nbsp;&nbsp;&nbsp;\");");
                output.WriteLine("write(\"<font style='cursor:hand;font-size:12px' onclick='fSetDate(giYear,giMonth,giDay)' onMouseOver='this.style.color=\\\"#0033FF\\\"' onMouseOut='this.style.color=0'>今天: \"+giYear+\"-\"+giMonth+\"-\"+giDay+\"</font>\");");
                output.WriteLine("write(\"</TD></TR></TD></TR></TABLE>\");");
                output.WriteLine("write(\"</Div>\");");
                output.WriteLine("}");
                output.WriteLine("</script>");
                output.WriteLine("<SCRIPT event=onclick() for=document>PopCal.style.visibility = 'hidden';</SCRIPT>");
                this.Page.RegisterClientScriptBlock("clientScript", "");
            }
        }

        [Bindable(true), Description("属性对应的中文名，目的是在数据校验的时候抛出异常提示时使用"), Browsable(true), Category("HFServer")]
        public string ChineseName
        {
            get
            {
                if (base.Attributes["ChineseName"] != null)
                {
                    this.strChineseName = base.Attributes["ChineseName"].Trim();
                }
                return this.strChineseName;
            }
            set
            {
                this.strChineseName = value;
                base.Attributes.Add("ChineseName", this.strChineseName);
            }
        }

        [DefaultValue(0), Bindable(true), Description("控件对应数据库字段的长度"), Browsable(true), Category("HFClient")]
        public string FieldLength
        {
            get
            {
                if (base.Attributes["FieldLength"] != null)
                {
                    this.strFieldLength = base.Attributes["FieldLength"].Trim();
                }
                return this.strFieldLength;
            }
            set
            {
                this.strFieldLength = value;
                base.Attributes.Add("FieldLength", this.strFieldLength);
            }
        }

        [Bindable(true), Category("HFClient"), Browsable(true), Description("选择True，表示该控件对应的数据库字段非空"), DefaultValue("False")]
        public NotNUllType NotNUll
        {
            get
            {
                return this._NotNUllType;
            }
            set
            {
                this._NotNUllType = value;
                base.Attributes.Add("NotNUll", this._NotNUllType.ToString());
            }
        }

        [Description("对应实体类属性的名称"), Bindable(true), Category("HFServer"), Browsable(true)]
        public string PropertyName
        {
            get
            {
                if (base.Attributes["PropertyName"] != null)
                {
                    this.strPropertyName = base.Attributes["PropertyName"].Trim();
                }
                return this.strPropertyName;
            }
            set
            {
                this.strPropertyName = value;
                base.Attributes.Add("PropertyName", this.strPropertyName);
            }
        }

        [Description("保存控件对应数据库字段当前行的原值"), Bindable(true), Category("HFServer"), Browsable(true)]
        public string PropertyValue
        {
            get
            {
                if (base.Attributes["PropertyValue"] != null)
                {
                    this.strPropertyValue = base.Attributes["PropertyValue"];
                }
                return this.strPropertyValue;
            }
            set
            {
                this.strPropertyValue = value;
                base.Attributes.Add("PropertyValue", this.strPropertyValue);
            }
        }

        [DefaultValue(""), Bindable(true), Category("Appearance")]
        public string Size
        {
            get
            {
                return this.size;
            }
            set
            {
                this.size = value;
            }
        }

        [Category("Appearance"), DefaultValue(""), Bindable(true)]
        public string Text
        {
            get
            {
                if (this.ViewState["Text"] != null)
                {
                    return this.ViewState["Text"].ToString();
                }
                return null;
            }
            set
            {
                this.ViewState["Text"] = value;
            }
        }

        public enum NotNUllType
        {
            False,
            True
        }
    }
}

